package com.omt.tarjimdinek.domain.user.dao;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Named;
import javax.inject.Singleton;
import javax.persistence.Query;

import org.springframework.util.StringUtils;

import com.omt.tarjimdinek.domain.user.entity.ValidationMembre;
import com.omt.tarjimdinek.support.GenericDao;

/**
 * Validation Membre DAO implements
 * @author chabane
 *
 */
@Named("validationMembreDao")
@Singleton
public class ValidationMembreDao  extends GenericDao<ValidationMembre, Long> implements IValidationMembreDao {

    public ValidationMembreDao() {
        super(ValidationMembre.class);
    }
    
    /**
     * {@inheritDoc}
     */
    public ValidationMembre findValidationMembreCriteria(ValidationMembreCriteria aCriteria){
    	
    	ValidationMembre lValidationMembre = null;
    	
    	String lSelect = " SELECT ";
    	String lFROM = " FROM ";
    	String lDistinct = " DISTINCT ";
    	String lWhere = " WHERE ";
    	String lAnd = " AND ";
    	String lNextClause = " ";
    	
    	StringBuffer lRequete = new StringBuffer();
    	
    	StringBuffer lClauseSelect = new StringBuffer();
    	lClauseSelect.append(lSelect);
    	lClauseSelect.append(lDistinct);
    	lClauseSelect.append(" validationMembre ");
    	lClauseSelect.append(lFROM);
    	lClauseSelect.append(" ValidationMembre validationMembre ");
    	
    	StringBuffer lClauseWhere = new StringBuffer();
    	
    	if(StringUtils.hasLength(aCriteria.getEmail())){
    		lClauseWhere.append(" validationMembre.email = :email ");
    		lNextClause = lAnd;
    	}
    	
    	if(StringUtils.hasLength(aCriteria.getUuid())){
    		lClauseWhere.append(lNextClause);
    		lClauseWhere.append(" validationMembre.uuid = :uuid ");
    		lNextClause = lAnd;
    	}
    	
    	lRequete.append(lClauseSelect);
    	
    	if(lClauseWhere.length() > 0) {
    		lRequete.append(lWhere);
        	lRequete.append(lClauseWhere);
    	}
    	
    	
    	Query lQuery = getEntityManager().createQuery(lRequete.toString());
    	
    	if(StringUtils.hasLength(aCriteria.getEmail())){
    		lQuery.setParameter("email", aCriteria.getEmail());
    	}
    	
    	if(StringUtils.hasLength(aCriteria.getUuid())){
    		lQuery.setParameter("uuid", aCriteria.getUuid());
    	}
    	
    	lValidationMembre = (ValidationMembre) lQuery.getSingleResult();
    	
    	return lValidationMembre;
    }
}
